package com.panshi.tools;

import java.util.Map;
import java.util.HashMap;

public class ResponseData {

    /**
     *
     * 1.先200,取data
     * 2.非200算异常
     * 3.然后判断是否10001.
     * 4.非1001属于流程控制异常,取message
     * 5.如果是1001,属于业务异常,取data中的errors
     *
     *
     * 200示例
     *
     {
     "code": 200,
     "data": {这里是返回的数据},
     "message": "ok"
     }
     *
     *
     * 非200,非1001示例
     {
     "code": 403,
     "data": {},
     "message": "登录验证过期,请重新登录!"
     }
     *
     *
     {
     "code": 1001,
     "data": { "errors": "您的提现额度已满,请明日再试"},
     "message": "Customer Error!"
     }
     *
     *
     */

    public static final String ERRORS_KEY = "errors";

    private final String message;
    private final int code;
    private final Map<String, Object> data = new HashMap<String, Object>();

    public String getMessage() {
        return message;
    }

    public int getCode() {
        return code;
    }

    public Map<String, Object> getData() {
        return data;
    }

    public ResponseData putDataValue(String key, Object value) {
        data.put(key, value);
        return this;
    }

    private ResponseData(int code, String message) {
        this.code = code;
        this.message = message;
    }

    public static ResponseData ok() {
        return new ResponseData(200, "Ok");
    }

    public static ResponseData notFound() {
        return new ResponseData(404, "所请求的资源没有找到!");
    }

    public static ResponseData badRequest() {
        return new ResponseData(400, "Bad Request");
    }

    public static ResponseData forbidden() {
        return new ResponseData(403, "登录验证过期,请重新登录!");
    }

    public static ResponseData unauthorized() {
        return new ResponseData(401, "unauthorized");
    }

    public static ResponseData serverInternalError() {
        return new ResponseData(500, "系统内部错误,请联系客服解决!");
    }

    public static ResponseData customerError() {
        return new ResponseData(1001, "提示信息");
    }






}